package parser

import (
	"fmt"
	"github.com/thedevsaddam/gojsonq"
	"webProject/crawler"
	"webProject/crawler/lagou/fetcher"
	"webProject/crawler/model"
)

func ParseJobFromJson(contents []byte) crawler.ParseResult{
	ContentsJson := gojsonq.New().JSONString(string(contents))
	//拿到sid
	showId := ContentsJson.Find("content.showId")
	s := showId.(string)
	fetcher.SIDchan <- s
	ContentsJson.Reset()
	//将sid发给用户
	size := ContentsJson.Find("content.positionResult.resultSize")
	i := int(size.(float64))
	//fmt.Print(i)
	var result []model.JobItem
	for k:=0;k<i;k++{
		path := fmt.Sprintf("content.positionResult.result.[%d]",k)
		//fmt.Println(path)
		ContentsJson.Reset()
		find := ContentsJson.Find(path)
		//fmt.Printf("%v \n",find)
		jobMap := find.(map[string]interface{})
		/**
		提取需要的元素
		职位：positionName
		公司名：companyShortName
		公司全名：companyFullName
		公司规模：companySize
		公司类型：industryField
		职位类型：firstType+secondType+thirdType
		工资：salary
		经验要求：workYear
		是否全职：jobNature
		学历要求：education
		城市：city
		地点：district
		*/
		district := jobMap["district"]
		if district==nil {
			district=""
		}
		item := model.JobItem{
			PositionName:     jobMap["positionName"].(string),
			CompanyShortName: jobMap["companyShortName"].(string),
			CompanyFullName:  jobMap["companyFullName"].(string),
			CompanySize:      jobMap["companySize"].(string),
			IndustryField:    jobMap["industryField"].(string),
			JobType:          jobMap["firstType"].(string)+jobMap["secondType"].(string)+jobMap["thirdType"].(string),
			Salary:           jobMap["salary"].(string),
			WorkYear:         jobMap["workYear"].(string),
			JobNature:        jobMap["jobNature"].(string),
			Education:        jobMap["education"].(string),
			City:             jobMap["city"].(string),
			District:         district.(string),
		}
		result = append(result,item)
	}

	return crawler.ParseResult{Items:result}
}